<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 合同 服务控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\service\ContractService;
use think\Request;

class ContractController extends VerifyController
{
    protected $contractService;

    /**
     * ContractTemplateController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->contractService = ContractService::getInstance();
    }

    /**
     * 生成合同编号
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function generateContractNumber(Request $request)
    {
        $res = $this->contractService->generateContractNumber();
        return json(success_response("", $res));
    }

    /**
     * 创建合同
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function create(Request $request)
    {
        $param = $request->param();

        // 框架合同乙方 为当前租户
        if ($param['data']['type'] == "contract_type_framework") {
            $param['data']['to_tenant_id'] = isset($param['data']['to_tenant_id']) && $param['data']['to_tenant_id'] > 0 ? $param['data']['to_tenant_id'] : $request->getTenantId();
        }

        // 授权签 甲方为当前租户
        if ($param['data']['type'] == "contract_type_before_authsign") {
            $param['data']['from_tenant_id'] = isset($param['data']['from_tenant_id']) && $param['data']['from_tenant_id'] > 0 ? $param['data']['from_tenant_id'] : $request->getTenantId();
        }

        $res = $this->contractService->create($param['data'], $request->getTenantId(), $request->getUserId());
        return json(success_response("", $res));
    }

    /**
     * 获取手动签署地址
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function extSign(Request $request)
    {
        $param = $request->param();
        $res = $this->contractService->extSign($param['param'], $request->getTenantId(), $request->getUserId());
        return json(success_response("", $res));
    }

    /**
     * 合同列表
     * @param Request $request
     * @throws \Exception
     */
    public function getContractList(Request $request)
    {
        $param = $request->param();
        $res = $this->contractService->getContractList($param['param'], $request->getTenantId());
        return json(success_response("", $res));
    }

    /**
     * 合同详情
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getContractDetail(Request $request)
    {
        $param = $request->param();
        $res = $this->contractService->getContractDetail($param['param']);
        return json(success_response("", $res));
    }


    /**
     * 取消合同
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function cancelContract(Request $request)
    {
        $param = $request->param();
        $res = $this->contractService->cancelContract($request->getTenantId(), $param['param']);
        return json(success_response("", $res));
    }

    /**
     * 合同状态查询
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function status(Request $request)
    {
        $param = $request->param();
        $res = $this->contractService->status($param['param']);
        return json(success_response("", $res));
    }

    /**
     * 基本合同列表
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getContractNormalList(Request $request)
    {
        $param = $request->param();
        $param['param']['filter']['contract.from_tenant_id'] = $param['param']['filter']['contract.from_tenant_id'] ?? $param['param']['filter']['from_tenant_id'] ?? $request->getTenantId();
        $res = $this->contractService->getContractNormalList($param['param']);
        return json(success_response("", $res));
    }

    /**
     * 框架合同默认合同时长配置
     * @param Request $request
     * @return \think\Response
     */
    public function getFrameworkContractDateConfig(Request $request)
    {
        $res = $this->contractService->getFrameworkContractDateConfig();
        return json(success_response("", $res));
    }
}
